import axios from "axios";

const accessToken = window.localStorage.getItem("AccessToken");

const axiosInstance = axios.create({
    // baseURL: 'http://wh011040.mysh360.com',
    // timeout: 5000,
    headers: { 'Authorization': accessToken }
})

//预设网络异常代码
const jsonHttpcode = {
    A100: 'Continue 继续',
    A101: 'Switching Protocols 切换协议',
    A400: 'Bad Request 客户端请求的语法错误',
    A401: 'Unauthorized 请求要求用户的身份认证',
    A403: 'Forbidden ',
    A404: 'Not Found'
}
//预设接口异常代码
const jsonApiCode = {
    Fail: 'This is fail',
    Error: 'This is error'
}

const jsonArray = window.sessionStorage.getItem("jsonArrayUserGroup");
const jsonArrayUserGroup = JSON.parse(jsonArray);
//函数封装
function reqFuncAxios(funIn) {
    let reqAttribute = {
        url: 'http://wh011040.mysh360.com/AttrUserAccount/QueryUserGroup',
        method: 'get'
    }
    if (jsonArrayUserGroup != null) {
        funIn(jsonArrayUserGroup[0].NumUserGroup)
    } else {
        const promiseReq = axiosInstance(reqAttribute);  //调用axios实例请求后端接口
        promiseReq.then((reResult) => {
            if (reResult.status === 200) {
                if (reResult.data.MsgType === "success") {
                    funIn(reResult.data.Payload.JsonArrayUserGroup[0].NumUserGroup)
                    window.sessionStorage.setItem('jsonArrayUserGroup', JSON.stringify(reResult.data.Payload.JsonArrayUserGroup))
                }
                else {
                    alert(jsonApiCode[reResult.data.StatusCode])
                }
            }
            else {
                alert(jsonHttpcode['A' + reResult.status])
            }
        })
    }
}

export default reqFuncAxios